package com.such.kit.web.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.such.kit.validate.ValidateCommons;

/**
 * @author SUCH
 * <pre>
 * 为前端制定的响应对象
 * </pre>
 */
public class WebResponse<T> implements Serializable {

	/** WEB_RESPONSE_CODE_200：成功码 */
	public static final int WEB_RESPONSE_CODE_200 = 200;
	/** WEB_RESPONSE_CODE_251：警告码，展示消息 */
	public static final int WEB_RESPONSE_CODE_251 = 251;
	/** WEB_RESPONSE_CODE_500：错误码，展示消息并中断后续操作 */
	public static final int WEB_RESPONSE_CODE_500 = 500;

	private static final long serialVersionUID = 188422628637690039L;

	/** code：响应码 [默认值为 {@link #WEB_RESPONSE_CODE_200}] [{@link #WEB_RESPONSE_CODE_200} | {@link #WEB_RESPONSE_CODE_251} | {@link #WEB_RESPONSE_CODE_500}] */
	private int code = WEB_RESPONSE_CODE_200;
	/** messages：消息集 */
	private List<String> messages;
	/** item：响应主体 */
	private T item;

	public WebResponse() {
	}

	public WebResponse(T item) {
		this.item = item;
	}

	public WebResponse(int code, List<String> messages) {
		this.code = code;
		this.messages = messages;
	}

	public WebResponse(T item, int code, List<String> messages) {
		this.item = item;
		this.code = code;
		this.messages = messages;
	}

	public WebResponse(int code, String... messages) {
		this.code = code;
		this.messages = Arrays.stream(messages).collect(Collectors.toList());
	}

	public WebResponse(T item, int code, String... messages) {
		this(code, messages);
		this.item = item;
	}

	/** [参考 {@link #code}] */
	public int getCode() {
		return this.code;
	}

	/** [参考 {@link #code}] */
	public void setCode(int code) {
		this.code = code;
	}

	/** [参考 {@link #messages}] */
	public List<String> getMessages() {
		return this.messages;
	}

	/** [参考 {@link #messages}] */
	public void setMessages(List<String> messages) {
		this.messages = messages;
	}

	/**
	 * <pre>
	 * 添加消息
	 * </pre>
	 * @param message 消息
	 */
	public void addMessage(String message) {
		if (ValidateCommons.isEmpty(this.messages)) {
			this.messages = new ArrayList<String>();
		}
		this.messages.add(message);
	}

	/** [参考 {@link #item}] */
	public T getItem() {
		return this.item;
	}

	/** [参考 {@link #item}] */
	public void setItem(T item) {
		this.item = item;
	}

	/**
	 * <pre>
	 * 获取 web 响应对象的响应主体（既需要响应的数据）
	 * </pre>
	 * @return
	 */
	public Object getResponseBody() {
		return this.item;
	}

}
